package br.ufu.si.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Example;

@SuppressWarnings({"rawtypes", "unchecked"})
public class GenericDAO <T>{
	
	public Session getSession() {
		return HibernateSessionUtil.getSession();
	}
	
	public void close(){
		getSession().close();
	}
	
	public void beginTransaction(){
		getSession().beginTransaction();
	}
	
	public void commit() {
		getSession().getTransaction().commit();
	}
	
	public void rollback(){
		getSession().getTransaction().rollback();
	}
	
	public void saveOrUpdate(Object object){
		getSession().saveOrUpdate(object);
	}
	
	public void save(Object objeto) {
		getSession().save(objeto);
	}

	
	
	public List<T> findByExample(T ex){
		Criteria criteria = getSession().createCriteria(ex.getClass());
		
		Example example = Example.create(ex);
		example.excludeNone();
		
		criteria.add(example);
		
		return criteria.list();
	}
	
	public List<T> findAll(Class clazz) {
		Criteria criteria = getSession().createCriteria(clazz);
		
		return criteria.list();
	}

}
